//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Threading;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using Microsoft.TridentWordAddIn.ResourceHelper;
using Microsoft.TridentWordAddIn.Server;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// Download Pipeline Zip
    /// </summary>
    internal partial class DownloadZipForm : BaseForm
    {
        private bool canceled;
        private int documentKey;
        private string fileName;
        private Guid workflowId;
        private Thread downloadThread;
        private ITridentServer tridentServer;

        /// <summary>
        /// Gets the exception message.
        /// </summary>
        /// <value>The exception message.</value>
        public string ExceptionMessage { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DownloadZipForm"/> class.
        /// </summary>
        public DownloadZipForm()
        {
            InitializeComponent();
        }

        #region Public Methods

        /// <summary>
        /// Sets the work.
        /// </summary>
        /// <param name="documentKey">The document key.</param>
        /// <param name="filename">The filename.</param>
        /// <param name="guid">The GUID.</param>
        /// <param name="server">The server.</param>
        public void SetWork(int documentKey, string filename, Guid guid, ITridentServer server)
        {
            this.workflowId = guid;
            this.fileName = filename;
            this.documentKey = documentKey;
            this.tridentServer = server;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Exports the workflow package.
        /// </summary>
        private void ExportWorkflowPackage()
        {
            try
            {
                IController controller = Controller.Instance(null);
                controller.ExportPackage(this.documentKey, this.fileName, this.workflowId, this.tridentServer);
            }
            catch (NoRecordFoundException)
            {
              this.ExceptionMessage = string.Format("OPC package with id ({0}) not found at the server.", this.workflowId);
            }
            catch (ConnectionFailedException connException)
            {
                this.ExceptionMessage = connException.Message;
            }
            catch (ThreadAbortException) {}
            catch (Exception e)
            {
                this.ExceptionMessage = e.Message;
            }
        }

        /// <summary>
        /// Handles the Shown event of the DownloadZipForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void DownloadZipForm_Shown(object sender, EventArgs e)
        {
            this.downloadThread = new Thread(new ThreadStart(this.ExportWorkflowPackage));
            this.downloadThread.Start();
            this.timer.Start();
        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.canceled = true;
            if (this.downloadThread.IsAlive)
            {
                this.downloadThread.Abort();
            }
        }

        /// <summary>
        /// Handles the Tick event of the timer1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (!this.downloadThread.IsAlive)
            {
                this.timer.Stop();
                if (this.canceled)
                {
                    FileHelper.DeleteFile(this.fileName);
                }

                this.Close();
            }
        }

        #endregion
    }
}